package com.sharding5.mybatis.seata.algorithm;

import com.google.common.collect.Range;
import com.google.common.collect.Sets;
import org.apache.shardingsphere.sharding.api.sharding.standard.*;

import java.util.*;

/**
 * @author lenovo
 */
public class MyDatabaseShardingAlgorithm implements StandardShardingAlgorithm<Long> {
    @Override
    public String doSharding(Collection<String> collection, PreciseShardingValue<Long> preciseShardingValue) {
        Long curValue = preciseShardingValue.getValue();
        String curBase = "";
        if (curValue > 0 && curValue <= 200) {
            curBase = "saleorder01";
        } else {
            curBase = "saleorder02";
        }
        return curBase;
    }

    @Override
    public Collection<String> doSharding(Collection<String> collection, RangeShardingValue<Long> rangeShardingValue) {
        Range<Long> valueRange = rangeShardingValue.getValueRange();
        Long lowerEndpoint = valueRange.lowerEndpoint();
        Long upperEndpoint = valueRange.upperEndpoint();

        Set<String> curBase = Sets.newHashSet();
        if (upperEndpoint <= 200) {
            curBase.add("saleorder01");
            return curBase;
        }
        if (lowerEndpoint > 200) {
            curBase.add("saleorder02");
            return curBase;
        }
        curBase.add("saleorder01");
        curBase.add("saleorder02");
        return curBase;
    }

    @Override
    public void init(Properties props) {
    }

    @Override
    public String getType() {
        return "MY_DATABASE_SHARDING_ALGORITHM";
    }
}